package mobidever.mygodutch.database.sqlitedal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import mobidever.mygodutch.R;
import mobidever.mygodutch.database.base.SQLiteDALBase;
import mobidever.mygodutch.model.ModelAccountBook;
import mobidever.mygodutch.utility.DateTools;

public class SQLiteDALAccountBook extends SQLiteDALBase
{

	public SQLiteDALAccountBook(Context pContext)
	{
		super(pContext);
	}

	public boolean insertAccountBook(ModelAccountBook pModelAccountBook)
	{
		ContentValues ContentValues = modelAccountBookChangeToContentValues(pModelAccountBook);
		long _ID = getDatabase().insert(getTableNameAndPK()[0], null,
				ContentValues);
		pModelAccountBook.setAccountBookID(_ID);
		return _ID >= 0;
	}

	public boolean deleteAccountBook(String pCondition)
	{
		return getDatabase().delete(getTableNameAndPK()[0], pCondition, null) > 0;
	}

	public boolean updateAccountBook(ModelAccountBook pModelAccountBook)
	{
		long _id = pModelAccountBook.getAccountBookID();
		ContentValues ContentValues = modelAccountBookChangeToContentValues(pModelAccountBook);
		return getDatabase().update(getTableNameAndPK()[0], ContentValues,
				"AccountBookID=" + _id, null) > 0;
	}

	public boolean updateAccountBook(ModelAccountBook pModelAccountBook, String pCondition)
	{
		ContentValues ContentValues = modelAccountBookChangeToContentValues(pModelAccountBook);
		return getDatabase().update(getTableNameAndPK()[0], ContentValues,
				"and AccountBookID=" + pCondition, null) > 0;
	}
	
	public boolean updateAccountBook(ContentValues pContentValues,String pCondition)
	{
		return getDatabase().update(getTableNameAndPK()[0], pContentValues, pCondition, null) > 0;
	}

	public List<ModelAccountBook> getAccountBooks(String pCondtition)
	{
		String _sql = "select * from " + getTableNameAndPK()[0] + " where 1=1 "
				+ pCondtition;
		ArrayList<ModelAccountBook> _ArrayList = (ArrayList<ModelAccountBook>) getList(_sql);
		return _ArrayList;
	}

	private ContentValues modelAccountBookChangeToContentValues(ModelAccountBook pModelAccountBook)
	{
		ContentValues ContentValues = new ContentValues();
		ContentValues.put("AccountBookName", pModelAccountBook.getAccountBookName());
		ContentValues.put("CreateDate", DateTools.getFormatDateTime(
				pModelAccountBook.getCreateDate(), "yyyy-mm-dd hh:mm:ss"));
		ContentValues.put("State", pModelAccountBook.getState());
		ContentValues.put("IsDefault", pModelAccountBook.getIsDefault());
		return ContentValues;
	}

	private void createDefaultAccountBook(SQLiteDatabase pDatabase)
	{
		String[] _AccountBookName = getContext().getResources().getStringArray(
				R.array.InitDefaultDataAccountBookName);
		for (int i = 0; i < _AccountBookName.length; i++)
		{
			ModelAccountBook _ModelAccountBook = new ModelAccountBook();
			_ModelAccountBook.setAccountBookName(_AccountBookName[i]);
			_ModelAccountBook.setIsDefault(1);
			
			ContentValues ContentValues = modelAccountBookChangeToContentValues(_ModelAccountBook);
			long _ID = pDatabase.insert(getTableNameAndPK()[0], null,
					ContentValues);
			_ModelAccountBook.setAccountBookID(_ID);
		}
	}

	@Override
	protected String[] getTableNameAndPK()
	{
		return new String[]
		{ "AccountBook", "AccountBookID" };
	}

	@Override
	protected Object findModel(Cursor pCursor)
	{
		ModelAccountBook _ModelAccountBook = new ModelAccountBook();
		_ModelAccountBook.setAccountBookName(pCursor.getString(pCursor
				.getColumnIndex("AccountBookName")));
		_ModelAccountBook.setAccountBookID(pCursor.getInt(pCursor.getColumnIndex("AccountBookID")));
		Date _Date = DateTools.getDate(
				pCursor.getString(pCursor.getColumnIndex("CreateDate")),
				"yyyy-MM-dd HH:mm:ss");
		_ModelAccountBook.setCreateDate(_Date);
		_ModelAccountBook.setState(pCursor.getInt(pCursor.getColumnIndex("State")));
		_ModelAccountBook.setIsDefault(pCursor.getInt(pCursor.getColumnIndex("IsDefault")));
		return _ModelAccountBook;
	}

	public void onCreate(SQLiteDatabase pDatabase)
	{
		StringBuilder sCreateTableScript = new StringBuilder();

		sCreateTableScript.append("		Create  TABLE AccountBook(");
		sCreateTableScript.append("				[AccountBookID] integer PRIMARY KEY AUTOINCREMENT NOT NULL");
		sCreateTableScript.append("				,[AccountBookName] varchar(20) NOT NULL");
		sCreateTableScript.append("				,[CreateDate] datetime NOT NULL");
		sCreateTableScript.append("				,[State] integer NOT NULL");
		sCreateTableScript.append("				,[IsDefault] integer NOT NULL");
		sCreateTableScript.append("				)");
		pDatabase.execSQL(sCreateTableScript.toString());
		createDefaultAccountBook(pDatabase);
	}

	public void onUpdate(SQLiteDatabase pDatabase)
	{
		// TODO Auto-generated method stub

	}

}
